#include "game.h"
	
		
		
void mW_game_draw ()
{
	
	acquire_bitmap(mW_buffer_page);
	clear_to_color(mW_buffer_page, makecol(0x0, 0x0, 0x0));
	release_bitmap(mW_buffer_page);
	
	
	
	// draw the map
	if (mW_map_blit(mW_current_map, mW_buffer_page, SCREEN_W / mW_TILE_SIZE + 1, SCREEN_H / mW_TILE_SIZE + 1, 0, 0) == 0)
	{
		printf("Error drawing map\n");
		return;
	}
	
	
	// draw sprites
	if (mW_sprite_blit_all(mW_buffer_page) == 0)
	{
		printf("Error drawing sprites\n");
		return;
	}
	
	
	
	if (mW_centered_text != NULL)
	{
		acquire_bitmap(mW_buffer_page);
		textout_centre_ex(mW_buffer_page, font, mW_centered_text, SCREEN_W/2, SCREEN_H/2, makecol(0xFF, 0xFF, 0xFF), makecol(0x0, 0x0, 0x0));
		release_bitmap(mW_buffer_page);
	}
	
	
	
	mW_interface_draw(mW_buffer_page);
	
	
	
	mW_mouse_draw_pointer(mW_buffer_page);
	
	
	
	acquire_screen();
	blit(mW_buffer_page, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
	release_screen();
	
	mW_map_update_offsets(mW_current_map);
	mW_sprite_update();
	
}



int mW_game ()
{
	
	mW_buffer_page = create_bitmap(SCREEN_W, SCREEN_H);
	
	
	// initialize some values
	mW_centered_text = NULL;


	// load the test map
	mW_current_map = mW_map_load_file("data/maps/test");
	if (mW_current_map == NULL)
	{
		printf("Error loading map\n");
		return 0;
	}
	mW_map_set_display_dimensions(mW_current_map, SCREEN_W / mW_TILE_SIZE, SCREEN_H / mW_TILE_SIZE);


	// install a timer to redraw the map
	if (install_int_ex(mW_game_draw, MSEC_TO_TIMER(33)) != 0)
	{
		printf("Error setting map draw timer\n");
		return 0;
	}
	
	
	mW_player_sprite = mW_sprite_create();
	mW_sprite_load_config(mW_player_sprite, "data/sprites/gialdini/config.lua");
	mW_sprite_center_map(mW_player_sprite, mW_current_map);
	
	
	
	int sprite_star;
	int sprite_trev;
	
	sprite_trev = mW_sprite_create();
	mW_sprite_load_config(sprite_trev, "data/sprites/trev/config.lua");

	sprite_star = mW_sprite_create();
	mW_sprite_load_config(sprite_star, "data/sprites/star/config.lua");
	
	
	
	mW_game_loop();
	
	
	remove_int(mW_game_draw);
	
	rest(33);
	
	mW_sprite_destroy(sprite_star);
	mW_sprite_destroy(sprite_trev);
	mW_sprite_destroy(mW_player_sprite);

	
	free(mW_buffer_page);
	
	return 1;

}



int mW_game_loop ()
{
		
	int keep_looping_game = 1;
	char key;
	int interface_input;


	// game loop
	while (keep_looping_game)
	{
	
		while (keypressed())
		{

			key = mW_keyboard_get_char();

	
			// return key
			if (key == 13)
				mW_game_text_interface();
	
			// escape key
			if (key == 27)
			{
				keep_looping_game = 0;
				break;
			}
			
			else if (key == 'a')
				mW_map_scroll(mW_current_map, -1, 0);
			else if (key == 'd')
				mW_map_scroll(mW_current_map, 1, 0);
			else if (key == 'w')
				mW_map_scroll(mW_current_map, 0, -1);
			else if (key == 's')
				mW_map_scroll(mW_current_map, 0, 1);
				
		}
		
		
		interface_input = mW_interface_input();
		if (interface_input == 2)
			mW_game_select_input();
		else
			mW_game_mouse_click(mW_mouse_get_last_click());
		
		mW_mouse_delete_all_clicks();
		
		rest(10);
		
	}
	
	return 1;
	
}



int mW_game_mouse_click (mW_mouse_click * mouse_click)
{
	
	int tile_x;
	int tile_y;
	
	if (mouse_click == NULL)
		return 0;

	tile_x = mW_map_get_x_tile(mW_current_map, mouse_click->x);
	tile_y = mW_map_get_y_tile(mW_current_map, mouse_click->y);
	
	if ((tile_x >= 0) && (tile_y >= 0))
	{
		
		mW_sprite_move_to(mW_player_sprite, (unsigned short) tile_x, (unsigned short) tile_y);
		
	}
	
	return 1;
	
}



int mW_game_select_input ()
{
	
	mW_interface_selectbox_event * selectbox_event;
	
	selectbox_event = mW_interface_selectbox_get_event();
	
	if (selectbox_event == NULL)
		mW_error("mW_game_select_input selectbox_event was null");

	return 1;
	
}



int mW_game_text_interface ()
{

	char * command_string;


	command_string = mW_keyboard_input_string(32);
	
	if (strcmp(command_string, "mapeditor") == 0)
	{
		free(command_string);
		mW_mapeditor_loop();
		return 1;
			
	}
	else
	{
		free(command_string);
		return 0;
	}

}
